
/*
* @file models/pdtTrqde.js
*		产品管理/查看产品交易
*/

import { routerRedux } from 'dva/router';
import api from '../api';
import moment from 'moment';

const nbeginDate = moment(new Date()).format('YYYYMMDD');
const nendDate = moment(new Date()).format('YYYYMMDD')
export default {
	namespace:'pdttrade',
	state:{
		poslist:[],
	},
	subscriptions:{
		setup({dispatch,history}) {
			history.listen(({pathname,query}) => {
				if( pathname === '/systemMgt/accountMgt/pdtMgt/viewtrade'){
					dispatch({
						type: 'getList',
						payload: {
							...query,
						}
					})
				}
			})
		}
	},
	effects:{
	*	getList({payload:query},{call,put}){
			yield put({
				type:'initData',
				payload:{
					poslist:[]
				}
			})
			const response = yield call(api.getPdtTradeList, query);
				yield put({
					type: 'getListSuccess',
					payload: response ,
				});
		},
		*changeRoute({payload:query},{call,put}){
			yield put(routerRedux.replace({
				pathname:`/systemMgt/accountMgt/pdtMgt/viewtrade`,
				query:{
					...query,
				}
			}))
		}
	},
	reducers:{
		setState(state, {payload}) {
			return {
				...state,
				...payload
			}
		},
		initData(state, {payload}) {
			return {
				...state,
				...payload
			}
		},
		getListSuccess(state, {payload}) {
			const {data,page} = payload
			data.forEach((item) => {
				if(item.futuresValue === 'N/A'){
					item.futuresValueText = '';
				}else{
					item.futuresValueText = item.futuresValue;
				}
				
			});
			const status=data ? true:false
			return {
				...state,
				status,
				tradelist:data,
				page,
			}
		},
	}
}